import io.*;
import java.io.*;

public class OopdAssignment {

	public static void main (String [] args) {
		System.out.println("\f"); // There is no universal method of clearing the console window that I could find for Java, so this only works for most Unix terminals
		String logName = getLogName();
		viewAscii(logName);
	} // End Main

	public static int displayMenu (String logName) {
		int choice = 0; //Sets the variable 'choice' to 0
		int x = 0; //Sets the variable 'x' to 0
		while(x == 0){
			System.out.println("------- Menu -------");
			System.out.println(
				"******************** \n" +
				"*  1) New Entry    *\n" +
				"*  2) View Entries *\n" +
				"*  3) View Logo    *\n" +
				"*  4) Exit         *\n" +
				"********************\n"
			);  
 		choice = ConsoleInput.readInt(); // Sets the variable 'choice' to a user defined integer
		switch (choice) { //Used to interpret and redirect the input
    case 1: //If the input = 1 THEN
			System.out.println("\f");//Clears the Console/Terminal
			newEntry(logName);
			x = 1; //Sets x to 1, breaking the WHILE LOOP
			break; //Breaks the SWITCH
		case 2:
			System.out.println("\f");
			viewEntries(logName);
			x = 1; //Sets x to 1, breaking the WHILE LOOP
			break; //Breaks the SWITCH
		case 3:
			System.out.println("\f");
			viewAscii(logName);
			x = 1; //Sets x to 1, breaking the WHILE LOOP
			break; //Breaks the SWITCH
		case 4:
			System.out.println("\f");
			exitApplication();
			x = 1; //Sets x to 1, breaking the WHILE LOOP
			break; //Breaks the SWITCH
		default: //Anything other than 1, 2, 3 or 4 will be directed here
			System.out.println("Error, Please Try Again");
			break; //Breaks the SWITCH
	} // End SWITCH
	} // End WHILE LOOP
	return choice;
	} //End displayMenu()
	
	public static String getLogName () {
		TextFile inputFile;
		IDMaker IDtest;
		IDtest = new IDMaker();
		String fileName;
		Boolean existCheck, openedOkay;
		fileName = ConsoleInput.readWord("Please enter the name of desired log"); // Sets the variable 'logName' to
		fileName = (fileName + ".csv");
		inputFile = new TextFile(fileName, "r");
		existCheck = inputFile.openFile();
		if (existCheck == false) {
			System.out.println("Log did not exist. Creating log now.");
			TextFile newFile;
			newFile = new TextFile(fileName, "w");
			openedOkay = newFile.openFile();
			System.out.println(openedOkay);
			newFile.rewind();
			newFile.closeFile();
			IDtest.makeNewID(fileName);
			return fileName;
		} else {
				return fileName;
		}
	}
	
	public static void newEntry (String logName) {
		System.out.println ("Do New Entry");
		IDMaker IDtest;
		String streetName, streetType, lotName, guardComment, day, month, year, hourTime, minTime;
		int id, lotNumber, postCode;
		int x = 0;
		char guardType = 'S';
		id = (1000);
		FileAppender fileOutput;
		DateTimeClass dateTimeNow;
		LogClass inputData;
  		dateTimeNow = new DateTimeClass();
		fileOutput = new FileAppender();
		int option = 0; //Sets the variable 'option' to 0
		System.out.println("------- Enter -------");
		System.out.println(
			"******************** \n" +
			"*  1)Security Guard*\n" +
			"*  2)Police Officer*\n" +
			"********************\n"
		);  
 		option = ConsoleInput.readInt(); // Sets the variable 'option' to a user defined integer
		if (option == 1){
			guardType = 'S';
		} else if (option == 2) {
			guardType = 'P';
		}

        lotNumber = ConsoleInput.readInt("House/Plot Number");
		String lotNumberString = Integer.toString(lotNumber);
		lotName = ConsoleInput.readWord("House Name/Letter");
		streetName = ConsoleInput.readWord("Street Name");
		streetType = ConsoleInput.readWord("Street Type");
		lotNumberString = (lotNumberString + lotName);
		streetName = ( streetName + " " + streetType);
		postCode = 1234;
		while (x == 0) {
		postCode = ConsoleInput.readInt("Please enter postcode");
		if (postCode > 0 && postCode < 8000) {
		x = 1; }
		}
		IDtest = new IDMaker();
		
		IDtest.IDMaker(logName);
		id = IDtest.getNewID();
		day = dateTimeNow.day();
		month = dateTimeNow.month();
		year = dateTimeNow.year();
		hourTime = dateTimeNow.hour();
		minTime = dateTimeNow.minute();
		guardComment = ConsoleInput.readWord("Please enter a comment or Reference Number");
		fileOutput.makeFileAppender(guardType, id, lotNumberString, streetName, postCode,day, month, year, hourTime, minTime, guardComment, logName);
		System.out.println(guardType + " " + id + " " + lotNumberString + " " + streetName + " " + postCode + " " + day + " " + month + " " + year + " " + hourTime + " " + minTime + " " + guardComment);
 
	// new fileOutput(guardType, id, lotNumber, streetName, postCode, guardComment);
		System.out.println ("Entry added to " + logName);
		displayMenu(logName);
	} // End newEntry()

	

    public static void viewEntries(String logName) {
		System.out.println ("View Entries");
		System.out.println ("View entries from " + logName);
		TextFile inputFile;
		inputFile = new TextFile(logName, "r");
		inputFile.openFile();
		while (inputFile.endOfFile() == false) {
		char c = inputFile.readChar();
		System.out.print(c);
		}
		System.out.println("");
		displayMenu(logName);
    }


	
  public static void viewAscii (String logName) {

    System.out.println (" ____                 ___");
	System.out.println ("/\\  _`\\             /'___\\");
	System.out.println ("\\ \\,\\ \\_\\     __   /\\ \\__/");
	System.out.println (" \\/_\\__ \\   /'__`\\ \\ \\ ,__\\/'__`\\");
	System.out.println ("   /\\ \\ \\ \\/\\ \\ \\.\\_\\ \\ \\_/\\  __/");
	System.out.println ("   \\ `\\____\\ \\__/.\\_\\\\ \\_\\\\ \\____\\");
	System.out.println ("    \\/_____/\\/__/\\/_/ \\/_/ \\/____/");
	System.out.println ("");
	System.out.println ("               __ _ ___");
	System.out.println ("              / _` / __|");
	System.out.println ("             | (_| \\__ \\");
	System.out.println ("              \\__,_|___/");
	System.out.println (" __  __");
	System.out.println ("/\\ \\/\\ \\");
	System.out.println ("\\ \\ \\_\\ \\    ___   __  __    ____     __    ____");
	System.out.println (" \\ \\  _  \\  / __`\\/\\ \\/\\ \\  /',__\\  /'__`\\ /',__\\");
	System.out.println ("  \\ \\ \\ \\ \\/\\ \\ \\ \\ \\ \\_\\ \\/\\__, `\\/\\  __//\\__, `\\");
	System.out.println ("   \\ \\_\\ \\_\\ \\____/\\ \\____/\\/\\____/\\ \\____\\/\\____/");
	System.out.println ("    \\/_/\\/_/\\/___/  \\/___/  \\/___/  \\/____/\\/___/");
    displayMenu(logName);

	}// End viewAscii()

  public static void exitApplication () {

    System.exit(0); // Exit the Application

	} // End exitApplication

	} // End OopdAssigment Class